package com.taldh.springframework.beanlifecycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.context.*;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.StringValueResolver;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;

public class BeanLifecycle implements BeanNameAware, BeanClassLoaderAware, BeanFactoryAware, EnvironmentAware,
        EmbeddedValueResolverAware, ResourceLoaderAware, ApplicationEventPublisherAware, MessageSourceAware,
        ApplicationContextAware, ServletContextAware, InitializingBean, DestructionAwareBeanPostProcessor,
        DisposableBean{

    /**
     * BeanNameAware 的 setBeanName，这个是第一个触发
     */
    @Override
    public void setBeanName(String s) {
        System.out.println("第一个执行 bean name = ");
    }


    /**
     * BeanClassLoaderAware 的 setBeanClassLoader
     */
    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        System.out.println("第二个执行  classLoader = ");
    }

    /**
     * BeanFactoryAware 的 setBeanFactory
     */
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("第三个执行 beanFactory = ");
    }

    /**
     * EnvironmentAware 的 setEnvironment
     */
    @Override
    public void setEnvironment(Environment environment) {
        System.out.println("第四个执行 environment = ");
    }

    /**
     * EmbeddedValueResolverAware 的 EmbeddedValueResolverAware
     */
    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        System.out.println("第五个执行 resolver = ");
    }

    /**
     * ResourceLoaderAware 的 setResourceLoader
     */
    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        System.out.println("第六个执行 resourceLoader = " );
    }

    /**
     * ApplicationEventPublisherAware 的 setApplicationEventPublisher
     */
    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        System.out.println("第七个执行 applicationEventPublisher = ");
    }

    /**
     * MessageSourceAware 的 setMessageSource
     */
    @Override
    public void setMessageSource(MessageSource messageSource) {
        System.out.println("第八个执行 messageSource = " );
    }

    /**
     * ApplicationContextAware 的 setApplicationContext
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("第九个执行 applicationContext = ");
    }

    /**
     * ServletContextAware 的 setServletContext
     */
    @Override
    public void setServletContext(ServletContext servletContext) {
        System.out.println("第十个执行 servletContext = " );
    }


    /**
     * InitializingBean 的 afterPropertiesSet
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("第十二个执行 = afterPropertiesSet");
    }

    /**
     * 指定 的 initMethod
     */
    public void initMethod() {
        System.out.println("第十三个执行 = initMethod");
    }


    /**
     * DestructionAwareBeanPostProcessor 的 postProcessBeforeDestruction
     */
    @Override
    public void postProcessBeforeDestruction(Object o, String s) throws BeansException {
        System.out.println("第一个执行的销毁方法 o = " );
        System.out.println("第一个执行的销毁方法 s = " );
    }

    /**
     * DisposableBean 的 destroy
     */
    @Override
    public void destroy() throws Exception {
        System.out.println("第二个执行的销毁方法 = destroy");
    }

    public void destroyMethod() {
        System.out.println("第三个执行的销毁方法 ");
    }
}
